package com.jingyi.system.service.impl;

import com.jingyi.common.core.domain.entity.WxResource;
import com.jingyi.common.core.domain.entity.WxUser;
import com.jingyi.common.core.domain.entity.WxUserMedicalRecord;
import com.jingyi.common.utils.SecurityUtils;
import com.jingyi.system.mapper.WxResourceMapper;
import com.jingyi.system.mapper.WxUserMapper;
import com.jingyi.system.mapper.WxUserMedicalRecordMapper;
import com.jingyi.system.service.IWxResourceService;
import com.jingyi.system.service.IWxUserMedicalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;

/**
 * 微信患者医疗记录 业务层处理
 *
 * @author jingyi
 */
@Service
public class WxUserMedicalRecordServiceImpl implements IWxUserMedicalRecordService {

    @Autowired
    private WxUserMedicalRecordMapper wxUserMedicalRecordMapper;

    @Autowired
    private WxResourceMapper wxResourceMapper;

    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private IWxResourceService wxResourceService;

    /**
     * 根据条件分页查询微信患者医疗记录
     *
     * @param record 微信患者医疗记录
     * @return 微信患者医疗记录集合信息
     */
    @Override
    public List<WxUserMedicalRecord> selectRecordList(WxUserMedicalRecord record) {
        List<WxUserMedicalRecord> wxUserMedicalRecords = wxUserMedicalRecordMapper.selectRecordList(record);
        if(!Objects.isNull(wxUserMedicalRecords)){
            wxUserMedicalRecords.forEach(record1 -> {
                record1.setWxUser(wxUserMapper.selectUserById(record1.getUserId()));
                record1.setResourceList(wxResourceMapper.selectResourceByRecordId(record1.getRecordId()));
            });
        }
        return wxUserMedicalRecords;
    }

    /**
     * 通过微信患者ID查询微信患者医疗记录
     *
     * @param userId 微信患者ID
     * @return
     */
    @Override
    public List<WxUserMedicalRecord> selectRecordByUserId(Long userId) {
        List<WxUserMedicalRecord> records = wxUserMedicalRecordMapper.selectRecordByUserId(userId);
        if(null!=records && records.size()>0){
            records.forEach(record -> {
                record.setResourceList(wxResourceMapper.selectResourceByRecordId(record.getRecordId()));
            });
        }
        return records;
    }

    /**
     * 通过微信患者ID和状态查询微信患者医疗记录
     *
     * @param userId 微信患者ID
     * @param state 状态
     * @return
     */
    @Override
    public WxUserMedicalRecord selectRecordByUserIdAndState(Long userId, String state) {
        WxUserMedicalRecord wxUserMedicalRecord = wxUserMedicalRecordMapper.selectRecordByUserIdAndState(userId,state);
        if(null != wxUserMedicalRecord){
            wxUserMedicalRecord.setResourceList(wxResourceMapper.selectResourceByRecordId(wxUserMedicalRecord.getRecordId()));
        }
        return wxUserMedicalRecord;
    }

    /**
     * 查询所有微信患者医疗记录
     *
     * @return 微信患者医疗记录列表
     */
    @Override
    public List<WxUserMedicalRecord> selectRecordAll() {
        return wxUserMedicalRecordMapper.selectRecordAll();
    }

    /**
     * 通过记录ID查询微信患者医疗记录
     *
     * @param recordId 微信患者医疗记录ID
     * @return 微信患者医疗记录对象信息
     */
    @Override
    public WxUserMedicalRecord selectRecordById(Long recordId) {
        WxUserMedicalRecord record = wxUserMedicalRecordMapper.selectRecordById(recordId);
        Optional.ofNullable(record).ifPresent(record1 -> {
            record1.setResourceList(wxResourceMapper.selectResourceByRecordId(recordId));
        });
        return record;
    }

    /**
     * 审核微信患者医疗记录
     * @param record 微信患者医疗记录对象信息
     * @return
     */
    @Override
    public int auditRecordInfo(WxUserMedicalRecord record){
        WxUserMedicalRecord entity = wxUserMedicalRecordMapper.selectRecordById(record.getRecordId());
        Optional.ofNullable(entity).ifPresent(r -> {
            WxUser wxUser = new WxUser();
            wxUser.setUserId(entity.getUserId());
            wxUser.setState(record.getState());
            wxUserMapper.updateUser(wxUser);
        });
        return wxUserMedicalRecordMapper.auditRecordInfo(record);
    }

    /**
     * 处理微信患者医疗记录
     * @param record 微信患者医疗记录对象信息
     * @return
     */
    @Override
    public int processRecordInfo(WxUserMedicalRecord record){
        WxUserMedicalRecord entity = wxUserMedicalRecordMapper.selectRecordById(record.getRecordId());
        Optional.ofNullable(entity).ifPresent(record1 -> {
            WxUser wxUser = new WxUser();
            wxUser.setUserId(entity.getUserId());
            wxUser.setState(record.getState());
            wxUserMapper.updateUser(wxUser);
        });
        return wxUserMedicalRecordMapper.processRecordInfo(record);
    }

    /**
     * 修改微信患者医疗记录信息
     *
     * @param record 微信患者医疗记录信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateRecord(WxUserMedicalRecord record) {
        if(null != record._getHave() && record._getHave().size()>0){
            wxResourceService.deleteResourceByRecordId(record.getRecordId());
            record._getHave().forEach(res->{
                WxResource wxResource = new WxResource();
                wxResource.setImgPath(res);
                wxResource.setCreateBy("微信接口同步");
                wxResource.setRecordId(record.getRecordId());
                wxResourceService.insertResource(wxResource);
            });
        }
        return wxUserMedicalRecordMapper.updateRecord(record);
    }

    /**
     * 新增微信患者医疗记录信息
     *
     * @param record 微信患者医疗记录信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertRecord(WxUserMedicalRecord record) {
        int flag = wxUserMedicalRecordMapper.insertRecord(record);
        if(null != record._getHave() && record._getHave().size()>0){
            record._getHave().forEach(res->{
                WxResource wxResource = new WxResource();
                wxResource.setImgPath(res);
                wxResource.setCreateBy("微信接口同步");
                wxResource.setRecordId(record.getRecordId());
                wxResourceService.insertResource(wxResource);
            });
        }
        return flag;
    }

    /**
     * 通过微信患者医疗记录ID删除患者
     *
     * @param recordId 微信患者医疗记录ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRecordById(Long recordId) {
        return wxUserMedicalRecordMapper.deleteRecordById(recordId);
    }

    /**
     * 批量删除微信患者医疗记录信息
     *
     * @param recordIds 需要删除的微信患者医疗记录ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRecordByIds(Long[] recordIds) {
        return wxUserMedicalRecordMapper.deleteRecordByIds(recordIds);
    }

    /**
     * 通过根据条件查询患者医疗记录信息
     *
     * @param record 患者医疗记录信息
     * @return 结果
     */
    public WxUserMedicalRecord getByMap(WxUserMedicalRecord record){
        return wxUserMedicalRecordMapper.getByMap(record);
    }
}
